package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private AttrValueService attrValueService;
    @Autowired
    private FacilityInfoService facilityInfoService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private FeeValueService feeValueService;
    @Autowired
    private LeaseTermService leaseTermService;
    @Autowired
    private LeaseAgreementService leaseAgreementService;
    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    @Override
    public IPage<RoomItemVo> pageApartmentItemByQueryVo(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageApartmentItemByQueryVo(roomItemVoPage, queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailVoById(Long id) {
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        // 获取房间所属公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.getApartmentItemVoById(roomInfo.getApartmentId());
        roomDetailVo.setApartmentItemVo(apartmentItemVo);


        // 获取房间的图片列表
        List<GraphVo> roomGraphVoList = graphInfoService.getGraphVoListByIdAndItemType(id, ItemType.ROOM);
        roomDetailVo.setGraphVoList(roomGraphVoList);

        // 获取房间的属性列表
        List<AttrValueVo> attrValueVoList = attrValueService.getRoomAttrValueVoListById(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        // 获取房间的配套列表
        List<FacilityInfo> facilityInfoList = facilityInfoService.getRoomFacilityInfoListById(id);
        roomDetailVo.setFacilityInfoList(facilityInfoList);

        // 获取房间的标签列表
        List<LabelInfo> roomLabelInfoList = roomLabelService.getRoomLabelInfoListById(id);
        roomDetailVo.setLabelInfoList(roomLabelInfoList);

        // 获取房间的支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeService.listByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);

        // 获取房间所在公寓的杂费列表
        List<FeeValueVo> feeValueVoList = feeValueService.getRoomFeeValueVoListById(roomInfo.getApartmentId());
        roomDetailVo.setFeeValueVoList(feeValueVoList);

        // 获取房间的租期列表
        List<LeaseTerm> leaseTermList = leaseTermService.getRoomLeaseTermListById(id);
        roomDetailVo.setLeaseTermList(leaseTermList);


        // 房间是否被删除
        Boolean isDelete = roomInfo.getIsDeleted() == 1;
        roomDetailVo.setIsDelete(isDelete);
        // 房间是否已入住
        Boolean isCheckIn = leaseAgreementService.getIsCheckInByRoomId(id);
        roomDetailVo.setIsCheckIn(isCheckIn);


        // 自动添加浏览历史记录
        browsingHistoryService.saveHistory(LoginUserContext.getLoginUser().getUserId(), id);
        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(Page<RoomItemVo> roomItemVoPage, Long id) {

        return roomInfoMapper.pageItemByApartmentId(roomItemVoPage, id);
    }


}




